# -*- encoding: utf-8 -*-

from django import forms
from django.utils.translation import ugettext as _

from metrics.models import IntegerProperty, MetricsView, properties
from metrics.forms import UniversalFloatField, TimeField

class Distance(IntegerProperty):
    """Represent a distance in meter"""
    MIN_VALUE = 0.1
    MAX_VALUE = 200
    @classmethod
    def display(cls, value):
        return "%g km" % (float(value)/1000)

    @classmethod
    def _update_grapher(cls, grapher, user):
        grapher.display_data_func = cls.display
        return grapher


    @classmethod
    def get_form(cls, data=None):
        class DistanceForm(forms.Form):
            value = UniversalFloatField(label=_(u'Distance'),
                                        required=False,
                                        min_value=cls.MIN_VALUE,
                                        max_value=cls.MAX_VALUE,
                                        help_text=_(u'in km'))
        return DistanceForm(data, prefix="distance")

    @classmethod
    def add_data(cls, user, date, metrics_view, data):
        valid = False
        form = cls.get_form(data)
        if form.is_valid():
            value = form.cleaned_data['value']
            if not value:
                valid = True
            else:
                int_value = long(value*1000)
                instance = cls(user=user,
                               date=date,
                               metrics_view=metrics_view,
                               value=int_value)
                instance.put()
                valid = True
        return valid, form
properties.add(Distance)
                
class Time(IntegerProperty):
    """Represent a time in seconds"""
    @classmethod
    def display(cls, value):
        value = int(value)
        hours = value / 3600
        minutes = (value%3600) / 60
        return "%sh%.2d" % (hours, minutes)

    @classmethod
    def _update_grapher(cls, grapher, user):
        grapher.display_data_func = cls.display
        grapher.unit_max = 3600
        grapher.unit_base = 60
        return grapher


    @classmethod
    def get_form(cls, data=None):
        class TimeForm(forms.Form):
            value = TimeField(label=_(u'Time'),
                              required=False,
                              help_text=_(u"e.g. 1h30, 1:30:15, 12'45''"))            
        return TimeForm(data, prefix="time")

properties.add(Time) 

class CardiacFrequency(IntegerProperty):
    """Represent the cardiac frequency in pulse per minute"""
    MIN_VALUE = 50
    MAX_VALUE = 220

    @classmethod
    def get_name(cls):
        """Return the name of the property."""
        return _(u'Heart rate')

    @classmethod
    def display(cls, value):
        return "%s bpm" % int(value)

    @classmethod
    def _update_grapher(cls, grapher, user):
        grapher.display_data_func = cls.display
        grapher.max_value = cls.MAX_VALUE
        grapher.min_value = cls.MIN_VALUE - 10
        return grapher

    @classmethod
    def get_form(cls, data=None):
        class CardiacForm(forms.Form):
            value = forms.IntegerField(label=_(u'Heart rate'),
                                       required=False,
                                       min_value = cls.MIN_VALUE,
                                       max_value = cls.MAX_VALUE,
                                       help_text=_(u'in pulse per minute'))
        return CardiacForm(data, prefix='heart_rate')


properties.add(CardiacFrequency)

class MaxCardiacFrequency(CardiacFrequency):
    """Represent the max cardiac frequency of a training"""

    @classmethod
    def get_name(cls):
        """Return the name of the property."""
        return _(u'Max heart rate')

    @classmethod
    def get_form(cls, data=None):
        class CardiacForm(forms.Form):
            value = forms.IntegerField(label=_(u'Maximum heart rate'),
                                       required=False,
                                       min_value = cls.MIN_VALUE,
                                       max_value = cls.MAX_VALUE,
                                       help_text=_(u'in pulse per minute'))
        return CardiacForm(data, prefix='max_heart_rate')

properties.add(MaxCardiacFrequency) 

class MeanCardiacFrequency(CardiacFrequency):
    """Represent the mean cardiac frequency of a training"""

    @classmethod
    def get_name(cls):
        """Return the name of the property."""
        return _(u'Mean heart rate')

    @classmethod
    def get_form(cls, data=None):
        class CardiacForm(forms.Form):
            value = forms.IntegerField(label=_(u'Mean heart rate'),
                                       required=False,
                                       min_value = cls.MIN_VALUE,
                                       max_value = cls.MAX_VALUE,
                                       help_text=_(u'in pulse per minute'))
        return CardiacForm(data, prefix='mean_heart_rate')

properties.add(MeanCardiacFrequency) 

class Cadence(IntegerProperty):
    MAX_VALUE = 170
    MIN_VALUE = 30
    """Represent a cadence in Round per minute"""
    @classmethod
    def get_name(cls):
        """Return the name of the property."""
        return _(u'Cadence')

    @classmethod
    def get_form(cls, data=None):
        class CadenceForm(forms.Form):
            value = forms.IntegerField(label=_(u'Cadence'),
                                       required=False,
                                       max_value = cls.MAX_VALUE,
                                       help_text=_(u'in rpm (round per minute)'))
        return CadenceForm(data, prefix='cadence')

properties.add(Cadence) 


class SportMetricsView(MetricsView):
    """Metrics view for sports"""
